//
//  MessageTableViewCell.m
//  MyWeChat
//
//  Created by 蔡欣东 on 15/10/5.
//  Copyright © 2015年 蔡欣东. All rights reserved.
//

#import "MessageTableViewCell.h"
#import "UILabel+UILabel_new1.h"
#import "UIImage+UIImage_new1.h"
@implementation MessageTableViewCell

- (void)awakeFromNib {
    
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        _avatar = [[UIImageView alloc]init];
        _messageBackgroundView = [[UIImageView alloc]init];
        _messageLabel = [[UILabel alloc]init];
        _messageLabel.font = [UIFont systemFontOfSize:16];
        _messageLabel.numberOfLines = 0;
        
        [self addSubview:_avatar];
        [self addSubview:_messageBackgroundView];
        [self addSubview:_messageLabel];
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}
+(NSString*)reuseIdentifier{
    return @"MessageCell";
}
+(CGFloat)heightForCell:(MessageInfo*)message{
    CGRect screenRect = [UIScreen mainScreen].bounds;
    
    CGSize labelSize = [UILabel sizeOgString:message.messageBody andFont:[UIFont systemFontOfSize:16] andMaxWith:screenRect.size.width-10-20-45*2];
    
    if (labelSize.height<45+10) {
        return 55;
    }else{
        return labelSize.height+5;
    }
}
-(void)setupMessageCell:(MessageInfo*)message andUser:(UserInfo*) user{
    CGRect screenRect = [UIScreen mainScreen].bounds;
    CGSize labelSize = [UILabel sizeOgString:message.messageBody andFont:self.messageLabel.font andMaxWith:screenRect.size.width-10-20-45*2];
    CGSize messageBackgroundSize = CGSizeMake(labelSize.width+10, labelSize.height);
    
    if ([message.user.name isEqual: user.name]) {
        self.avatar.frame = CGRectMake(screenRect.size.width-45-5, 5, 45, 45);
        self.avatar.image = [UIImage imageNamed:@"touxiang"];
        
        self.messageLabel.text = message.messageBody;
        self.messageLabel.frame = CGRectMake(screenRect.size.width-5-45-5-15-labelSize.width, 5, labelSize.width, labelSize.height);
        self.messageLabel.textAlignment = NSTextAlignmentRight;
        self.messageBackgroundView.image = [UIImage resizableImage:@"sc.9"];
        self.messageBackgroundView.frame = CGRectMake(screenRect.size.width-5-45-5-messageBackgroundSize.width, 5, messageBackgroundSize.width, messageBackgroundSize.height);
    }else{
        self.avatar.frame = CGRectMake(5, 5, 45, 45);
        self.avatar.image = [UIImage imageNamed:@"touxiang"];
        self.messageLabel.text = message.messageBody;
        
        self.messageLabel.frame = CGRectMake(5+45+5+15, 5, labelSize.width, labelSize.height);
        self.messageLabel.textAlignment = NSTextAlignmentLeft;
        
        self.messageBackgroundView.image = [UIImage resizableImage:@"s6.9"];
        self.messageBackgroundView.frame = CGRectMake(5+45+5, 5, messageBackgroundSize.width, messageBackgroundSize.height);
    
    
    
    
    }

}






- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
